package com.wtx.fitness.controller;

import com.wtx.fitness.common.CommonResult;
import com.wtx.fitness.service.ScoreService;
import com.wtx.fitness.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员统计分析控制器
 */
@RestController
@RequestMapping("api/admin/statistics")
public class StatisticsController {

  @Autowired
  private StatisticsService statisticsService;

  @Autowired
  private ScoreService scoreService;

  /**
   * 获取总体统计数据
   */
  @GetMapping("/overall")
  public CommonResult getOverallStatistics() {
    Map<String, Object> result = statisticsService.getOverallStatistics();
    return CommonResult.success(result);
  }

  /**
   * 获取项目合格率统计
   */
  @GetMapping("/item-pass-rate")
  public CommonResult getItemPassRateStatistics() {
    Map<String, Object> result = statisticsService.getItemPassRateStatistics();
    return CommonResult.success(result);
  }

  /**
   * 获取性别分布统计数据
   */
  @GetMapping("/gender-distribution")
  public CommonResult getGenderScoreDistribution() {
    Map<String, Object> result = statisticsService.getGenderScoreDistribution();
    return CommonResult.success(result);
  }

  /**
   * 获取成绩等级分布统计
   */
  @GetMapping("/item-score-grade")
  public CommonResult getItemScoreGradeDistribution() {
    Map<String, Object> result = new HashMap<>();

    // 直接获取项目分布数据（已包含原始成绩数据）
    List<Map<String, Object>> distribution = statisticsService.getItemScoreGradeDistribution();
    result.put("distribution", distribution);

    return CommonResult.success(result);
  }

  /**
   * 获取所有统计数据
   */
  @GetMapping("/all")
  public CommonResult getAllStatistics() {
    Map<String, Object> result = new HashMap<>();

    // 添加总体统计数据
    result.put("overall", statisticsService.getOverallStatistics());

    // 添加项目合格率数据
    result.put("passRate", statisticsService.getItemPassRateStatistics());

    // 添加成绩分布数据（已包含原始成绩数据）
    Map<String, Object> gradeDistribution = new HashMap<>();
    List<Map<String, Object>> distribution = statisticsService.getItemScoreGradeDistribution();
    gradeDistribution.put("distribution", distribution);

    result.put("gradeDistribution", gradeDistribution);

    return CommonResult.success(result);
  }
}